package net.love5z.common.utils;

import java.io.UnsupportedEncodingException;

public class StringUtils {

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean equalsIgnoreCase(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
    }

    public static String contact(String contactor, String... strings) {

        if (strings == null) {
            return null;
        } else {
            if (strings.length == 0) {
                return "";
            } else if (strings.length == 1) {
                return strings[0];
            } else {
                StringBuilder sb = new StringBuilder(strings[0]);
                for (int i = 1; i < strings.length; i++) {
                    sb.append(contactor).append(strings[i]);
                }

                return sb.toString();
            }
        }

    }

    public static byte[] toByteArray(String str) {
        if (!isBlank(str)) {
            try {
                return str.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
        }

        return new byte[0];
    }

    public static String fromByteArray(byte[] byteArray, String charSet) {
        if (byteArray != null && byteArray.length > 0) {
            try {
                return new String(byteArray, charSet);
            } catch (UnsupportedEncodingException e) {
            }
        }

        return "";
    }

}
